/*
 * $Id: JMap.java 24 2009-03-31 23:40:10Z aaron.day $
 */
package org.crazydays.gameplan.map.swing;


import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.Scrollable;
import javax.swing.SwingConstants;

import org.crazydays.gameplan.map.IGameMap;

/**
 * JMap is the visual representation of the game map.
 */
public class JMap
        extends Component
        implements Scrollable
{
	/** serial version uid */
	public final static long serialVersionUID =
	        "$Id: JMap.java 24 2009-03-31 23:40:10Z aaron.day $".hashCode();

	/** game map */
	protected IGameMap gameMap;

	/** max unit increment */
	protected int maxUnitIncrement = 1;

	/**
	 * JMap constructor.
	 * 
	 * @param gameMap Game map
	 */
	public JMap(IGameMap gameMap)
	{
		setGameMap(gameMap);
		setSize(getGameMap().getGeometry().getWidth(), getGameMap()
		        .getGeometry().getHeight());
	}

	/**
	 * Set game map.
	 * 
	 * @param gameMap Game map
	 */
	public void setGameMap(IGameMap gameMap)
	{
		this.gameMap = gameMap;
	}

	/**
	 * Get game map.
	 * 
	 * @return Game map
	 */
	public IGameMap getGameMap()
	{
		return gameMap;
	}

	/**
	 * Get preferred size.
	 * 
	 * @return Size
	 * @see java.awt.Component#getPreferredSize()
	 */
	public Dimension getPreferredSize()
	{
		return new Dimension(getGameMap().getGeometry().getWidth(),
		        getGameMap().getGeometry().getHeight());
	}

	/**
	 * Paint image.
	 * 
	 * @param graphics Graphics
	 * @see java.awt.Component#paint(java.awt.Graphics)
	 */
	public void paint(Graphics graphics)
	{
		graphics.drawImage(getGameMap().getImage(), 0, 0, null);
	}

	/**
	 * Get preferred scrollable viewport size.
	 * 
	 * @return Dimension
	 * @see javax.swing.Scrollable#getPreferredScrollableViewportSize()
	 * @see Component#getPreferredSize()
	 */
	public Dimension getPreferredScrollableViewportSize()
	{
		return getPreferredSize();
	}

	/**
	 * Get scrollable tracks viewport height.
	 * 
	 * @return Tracks
	 * @see javax.swing.Scrollable#getScrollableTracksViewportHeight()
	 */
	public boolean getScrollableTracksViewportHeight()
	{
		return false;
	}

	/**
	 * Get scrollable tracks viewport width.
	 * 
	 * @return Tracks
	 * @see javax.swing.Scrollable#getScrollableTracksViewportWidth()
	 */
	public boolean getScrollableTracksViewportWidth()
	{
		return false;
	}

	/**
	 * Get scrollable unit increment.
	 * 
	 * @param visibleRect Visible rectangle
	 * @param orientation Orientation
	 * @param direction Direction
	 * @return Increment
	 * @see javax.swing.Scrollable#getScrollableUnitIncrement(Rectangle,int,int)
	 */
	public int getScrollableUnitIncrement(Rectangle visibleRect,
	        int orientation, int direction)
	{
		// Get the current position.
		int currentPosition = 0;
		if (orientation == SwingConstants.HORIZONTAL) {
			currentPosition = visibleRect.x;
		} else {
			currentPosition = visibleRect.y;
		}

		// Return the number of pixels between currentPosition
		// and the nearest tick mark in the indicated direction.
		if (direction < 0) {
			int newPosition =
			        currentPosition
			                - (currentPosition / maxUnitIncrement)
			                * maxUnitIncrement;
			return (newPosition == 0) ? maxUnitIncrement
			        : newPosition;
		} else {
			return ((currentPosition / maxUnitIncrement) + 1)
			        * maxUnitIncrement - currentPosition;
		}
	}

	/**
	 * Get scrollable block increment.
	 * 
	 * @param visibleRect Visisble rectangle
	 * @param orientation Orientation
	 * @param direction Direction
	 * @return Increment
	 * @see javax.swing.Scrollable#getScrollableBlockIncrement(Rectangle,int,int)
	 */
	public int getScrollableBlockIncrement(Rectangle visibleRect,
	        int orientation, int direction)
	{
		if (orientation == SwingConstants.HORIZONTAL) {
			return visibleRect.width - maxUnitIncrement;
		} else {
			return visibleRect.height - maxUnitIncrement;
		}
	}
}
